layui.use(['layer', 'form', 'table', 'ztree', 'laydate', 'admin', 'ax', 'func', 'tree', 'util'], function () {
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ZTree = layui.ztree;
    var $ax = layui.ax;
    var laydate = layui.laydate;
    var admin = layui.admin;
    var func = layui.func;
    var tree = layui.tree;
    var util = layui.util;

    form.render('select')

    /**
     * 系统管理--Banner列表
     */
    var base = {
        tableId: "baseTable"    //表格id
    };

    //渲染时间选择框
    laydate.render({
        elem: '#timeLimit',
        range: true,
        max: Feng.currentDate()
    });

    /**
     * 初始化表格的列
     */
    base.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {type: 'numbers', title: '序号'},
            {field: 'banner_type', align: "center", sort: true, title: '栏目',templet:function(d) {
            	if(d.banner_type == 1){
					return "自制栏目";
				}else if(d.banner_type == 2){	
					return "链接栏目";
				}
            }},
            {field: 'statusName', align: "center", sort: true, title: 'banner图片',templet:function(d) {
            	return '<a class="layui-btn layui-btn-primary layui-btn-yes-c  layui-btn-xs" lay-filter="details" id="details" data-id="' + d.banner_id + '">查看</a>';
            }},
            {field: 'update_time', align: "center", sort: true, title: '最后修改日期'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 280}
        ]];
    };
    
    //绑定事件
    $(document).on('click', '#details', function(data) {
    	var bannerId = $(this).attr("data-id");
    	 func.open({
             type: 2,
             shadeClose: true,
             shade: 0.5,
             area: '300px',
             title: '查看详情',
             maxmin: true, //开启最大化最小化按钮
             content: Feng.ctxPath + "/modular/banner/details/" + bannerId,
             tableId: base.tableId
         });
    });
    
    // 用户订单列表
    base.onQueryDetails = function (data) {
    	func.open({
            type: 2,
            shadeClose: true,
            shade: 0.5,
            area: '800px',
            title: '编辑',
            maxmin: true, //开启最大化最小化按钮
            content: Feng.ctxPath + "/modular/banner/edit/" + data.banner_id,
            tableId: base.tableId
        });
    }


    // 渲染表格
    var tableResult = table.render({
        title: "用户列表",
        elem: '#' + base.tableId,
        url: Feng.ctxPath + '/modular/banner/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: base.initColumn()
    });
    
    form.on('select(status)', function(data){
   	 var queryData = {};
        queryData['bannerType'] = $("#status").find("option:selected").val();
        table.reload(base.tableId, {
            where: queryData, page: {curr: 1}
        });
   });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        base.search();
    });
    
    /**
     * 点击查询按钮
     */
    base.search = function () {
    	func.open({
            type: 2,
            shadeClose: true,
            shade: 0.5,
            area: '800px',
            title: '新增',
            maxmin: true, //开启最大化最小化按钮
            content: Feng.ctxPath + "/modular/banner/add",
            tableId: base.tableId
        });
    };

    // 工具条点击事件
    table.on('tool(' + base.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            base.onQueryDetails(data);
        } else if (layEvent === 'delete') {
        	layer.confirm("您确认删除该条资讯吗?", function() {
    			//调用ajax局部启动
    			$.ajax({
    				url : Feng.ctxPath + "/modular/banner/delete/"+data.banner_id,
    				type : "POST",
    				contentType: "application/json;charset=UTF-8",
    				async : false,
    				cache : false,
    				success : function(obj) {
    					if (obj != null) {
    						if (obj.success) {
    							layer.msg("操作成功");
    						} else {
    							layer.msg("操作失败");
    						}
    					}
    				}
    			});
    			tableResult.reload();
    			setTimeout("layer.close(layer.index);",2000);
    		});
        }
    });

});

